class Conversion:
    __name = 'Conversion'

    ''' 2维矩阵转化为字典 '''

    @staticmethod
    def arr2d_2_dict(arr):
        dictionary = {}
        for element in arr:
            key = element[0]
            value = element[1]
            dictionary.update({key: value})
        return dictionary

    ''' 图集合转化为邻接矩阵 '''

    @staticmethod
    def graph_arr_2_matrix(g, direction=False):
        v_set = g[0]  # 图的点集
        s_set = g[1]  # 图的边集

        new_s_set = []  # 图的邻接矩阵
        for start in v_set:  # 起始点
            line = [float('inf') for i in range(len(v_set))]
            for index in range(len(v_set)):
                end = v_set[index]  # 终点
                for key, value in s_set.items():
                    if direction:  # 有向图
                        if start == key[0] and end == key[1]:
                            line[index] = float(value)
                    else:  # 无向图
                        if start in key and end in key and start != end:
                            line[index] = float(value)
            new_s_set.append(line)
        g_matrix = [v_set, new_s_set]
        return g_matrix

    ''' 二部图集合转化为邻接矩阵 '''

    @staticmethod
    def bipartite_graph_arr_2_matrix(g):
        v_set1 = g[0][0]
        v_set2 = g[0][1]
        s_set = g[1]  # 边集

        new_s_set = []  # 图的邻接矩阵
        for start in v_set1:  # 起始点
            line = [0 for i in range(len(v_set2))]
            for index in range(len(v_set2)):
                end = v_set2[index]  # 终点
                for key, value in s_set.items():
                    if start in key and end in key and start != end:
                        line[index] = float(value)
            new_s_set.append(line)
        g_matrix = [[v_set1, v_set2], new_s_set]
        return g_matrix
